import { Component, OnInit, ViewChild } from '@angular/core';
import { ModalDirective } from 'ng2-bootstrap/modal';
import { flyIn } from '../../../animations/fly-in';
import { MyHttp } from '../../../service/MyHttp';
import { PageModel } from "../../../model/page-model";
import { GlobalVariable } from "../../../global.config";
@Component({
	selector: 'app-operationlist',
	templateUrl: './operationlist.component.html',
	styleUrls: ['./operationlist.component.css']
})

export class OperationlistComponent implements OnInit {
	@ViewChild('myModal') public myModal: ModalDirective;
	btype: number;
	public pageModel: PageModel = new PageModel();
	private page: number = 1;
	public keyWord: string = '';
	private key_word: string = '';
	public data;
	public policyType = GlobalVariable.POLICY_TYPE;
	public selectData:any = { policy_name: '', policy_type: '', large_price: '', medium_price: '', small_price: '', charge_price: '' };
	constructor(private myHttp: MyHttp) {
		this.getData(1);
	}

	ngOnInit() { }

	getData(page: number) {
		this.page = page;
		this.myHttp.post('policy/findAll?page=' + page + '&key_word=' + this.key_word, {}).subscribe(res => {
			if (res.errorCode == 0) {
				this.data = res.message.data;
			}
		}, err => { console.log(err) });
	}
	search() {
		this.key_word = this.keyWord;
		this.getData(1);
		this.pageModel.currentPage = 1;
	}
	createPolicy() {
		this.myHttp.post('policy/create', this.selectData).subscribe(res => {
			if (res.errorCode == 0) {
				alert('保存成功');
				this.getData(this.page);
				this.cancel();
			}
		});
	}
	updatePolicy() {
		this.selectData.policy_id = this.selectData.id;
		this.myHttp.post('policy/update', this.selectData).subscribe(res => {
			if (res.errorCode == 0) {
				alert('保存成功');
				this.getData(this.page);
				this.cancel();
			}
		});
	}
	deletePolicy(){
		this.myHttp.post('policy/delete',{policy_id: this.selectData.id}).subscribe(res => {
			if (res.errorCode == 0) {
				alert('删除成功');
				this.getData(this.page);
				this.cancel();
			}
		});
	}
	//选择了某条值,不能直接对selectData赋值,否则只是地址相同
	select(item) {
		this.selectData = JSON.parse(JSON.stringify(item));
	}
	cancel() {
		this.myModal.hide();
		this.selectData = { policy_name: '', policy_type: '', large_price: '', medium_price: '', small_price: '', charge_price: '' };
	}

}
